//创建班级相关小仓库
import { defineStore } from "pinia";
import { reqClassAll, reqClassAdd, reqClassList, reqClassById, reqClassDelete, reqClassUpdate } from "@/api/class";
import type {
    classInfoData, classListResponseData
    , classResponseData, classAllResponseData, classListFormData
} from "@/api/class/type";
import type{ ClassState } from "./types/types";
let useClassStore = defineStore('Class', {
    state: (): ClassState => {
        return {
            classes: []
        }
    },
    actions: {
        //获取所有班级
        async getClassAll() {
            let result:classAllResponseData=await reqClassAll();
            if(result.status == 0){
                this.classes=(result.data as classInfoData[])
                return  'ok'
            }else{
                return Promise.reject(new Error (result.msg))
            }
        },
        //分页获取班级
        async getClassList(data:classListFormData){
            let result :classListResponseData= await reqClassList(data);
            if(result.status == 0){
                this.classes=(result.data?.data as classInfoData[])
                return result.data
            }else{
                return Promise.reject(new Error (result.msg))
            }
        },
        //添加班级
        async addClass(data:classInfoData){
            let result :classResponseData= await reqClassAdd(data);
            if(result.status == 0){
                return "ok"
            }else{
                return Promise.reject(new Error (result.msg))
            }
        },
        //根据id查询班级
        async getClassById(_id:string){
            let result :classResponseData= await reqClassById(_id);
            if(result.status == 0){
                return result.data
            }else{
                return Promise.reject(new Error (result.msg))
            }
        },
        //更新班级信息
        async updateClass(data:classInfoData){
            let result :classResponseData= await reqClassUpdate(data);
            if(result.status == 0){
                return "ok"
            }else{
                return Promise.reject(new Error (result.msg))
            }
        },
        //删除班级
         async deleteClass(classId:string){
            let result :classResponseData= await reqClassDelete(classId);
            if(result.status == 0){
                return "ok"
            }else{
                return Promise.reject(new Error (result.msg))
            }
        },
    },
})
export default useClassStore
























